package etc;

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.Random;

import javax.swing.ImageIcon;

import panels.MapPanel;
import tiles.Tile;

public class Wompus {

    private MapPanel map;
    private Tile wompusTile;
    private Random rand;
    private int wompusPresent;
    private int randomX;
    private int randomY;
    private int value;
    private double wompusChance;

    public Wompus(MapPanel map, double wompusChance) {

	this.map = map;
	this.wompusTile = null;
	this.rand = new Random();
	this.wompusChance = wompusChance;
    }

    public boolean wompusPresent() {

	return (rand.nextInt((int) (1 / wompusChance)) == 0);

    }

    public void setWompus() {

	if (wompusTile != null)
	    wompusTile.setCursor(Cursor
		    .getPredefinedCursor(Cursor.DEFAULT_CURSOR));

	if (wompusPresent()) {

	    do {

		randomX = rand.nextInt(9);
		randomY = rand.nextInt(5);

		wompusTile = map.getTile(randomY, randomX);

	    } while (wompusTile.isTown());

	    value = 100 + rand.nextInt(201);

	    Toolkit toolkit = Toolkit.getDefaultToolkit();
	    ImageIcon icon = new ImageIcon(this.getClass().getResource(
		    "/images/wompus.png"));
	    Image image = icon.getImage();
	    Cursor c = toolkit.createCustomCursor(image, new Point(0, 0),
		    "WOMPUS");
	    wompusTile.setCursor(c);
	    wompusTile.setWompus(true);

//	    System.out.println(randomX + ", " + randomY);

	}

	if (wompusTile != null)
	    wompusTile.updateTooltipText();

    }

    public void findWompus(Cursor c) {
	if (wompusTile != null) {
	    wompusTile.setCursor(c);
	    wompusTile.setWompus(false);
	    wompusTile.updateTooltipText();
	    wompusTile = null;
	}
    }

    public int getValue() {
	return value;
    }
}
